import { deviceGetList } from "@/api/teaInfuser/device";
import { getMaterialList, getMaterialTypeList } from "@/api/cabinet/formula";
import { getReleaseSelect } from "@/api/teaInfuser/formula";

/**
 * 异步生成用于选择门店分组的组件配置。
 *
 * 该函数旨在配置一个el-tree-select组件，用于选择门店分组。它通过调用getGroupAssignList接口获取门店分组数据，
 * 并将这些数据配置到select对象的options属性中，最终返回这个配置对象。
 *
 * @param key {string} 分组ID的键名，默认为"group_id"。
 * @param option {object} 别的属性
 * @returns {Promise<{type: string, key: string, label: string, placeholder: string, checkStrictly: boolean, renderAfterExpand: boolean, props: {value: string, label: string}, options: any[]}>} 返回一个Promise，解析为一个包含el-tree-select组件配置的对象。
 */
export const storeGroupSelect = async (
  key = "parent_id",
  option?: any,
  isCheckbox?: boolean
) => {
  // 初始化select组件的配置对象
  // const select = {
  //   type: "el-tree-select",
  //   key,
  //   label: "门店分组",
  //   placeholder: "请选择门店分组",
  //   // checkStrictly: true,
  //   renderAfterExpand: false,
  //   defaultExpandedKeys: [],
  //   props: {
  //     value: "id",
  //     label: "name"
  //   },
  //   data: [],
  //   ...(isCheckbox
  //     ? {
  //         clearable: true,
  //         multiple: true,
  //         showCheckbox: true,
  //         collapseTags: true,
  //         collapseTagsTooltip: true,
  //         maxCollapseTags: 2,
  //         valueKey: "id",
  //         filterable: true,
  //         props: {
  //           value: "id",
  //           label: "name",
  //           class: "custom-node",
  //           children: "children"
  //         }
  //       }
  //     : {}),
  //   ...option
  // };

  const { props, ...rest } = option;
  const select = {
    type: "el-cascader",
    key,
    label: "device_manage.form.store_group",
    placeholder: "device_manage.form.select_store_group",
    collapseTags: true,
    maxCollapseTags: 1,
    collapseTagsTooltip: true,
    filterable: true,
    props: {
      value: "id",
      label: "name",
      children: "children",
      emitPath: false,
      multiple: isCheckbox,
      ...(props ?? {})
    },
    options: [],
    ...(rest ?? {})
  };

  // 调用接口获取门店分组列表
  const res = await getReleaseSelect({});

  // select.defaultExpandedKeys = res.data.map(item => item.id);

  select.options = res.data;

  // 返回配置完整的select组件对象
  return select;
};
/**
 * 异步函数deviceSelect用于生成设备选择的配置对象。
 * 该函数通过调用deviceGetList接口获取设备列表，并将其格式化为选择控件的选项数据。
 * @param {string} key - 用于指定选择控件的值键名，默认为"device_id"。
 * @returns {Promise<object>} 返回一个包含选择控件配置的对象，其中options属性为设备选项数据。
 */
export const deviceSelect = async (key = "device_id") => {
  // 初始化选择控件的配置对象
  const select = {
    type: "select",
    key,
    label: "device_manage.form.device_name",
    placeholder: "device_manage.form.device_name_placeholder",
    valueKey: "device_id",
    labelKey: "name",
    options: []
  };

  // 调用deviceGetList接口获取设备列表数据
  const res = await deviceGetList({
    start_page: 0,
    size: 0
  });

  // 将获取的设备列表数据赋值给选择控件的options属性
  select.options = res.data;

  // 返回配置完整的选择控件对象
  return select;
};

export const materialTypeSelect = async (key = "material_type") => {
  const select = {
    type: "select",
    key,
    label: "material.form.type",
    placeholder: "material.form.type_placeholder",
    valueKey: "id",
    labelKey: "name",
    options: []
  };

  const res = await getMaterialTypeList({
    start_page: 0,
    size: 0
  });

  select.options = res.data;

  return select;
};

export const materialTypeSelectForMaterial = async (key = "type_id") => {
  const select = {
    type: "select",
    key,
    label: "material.form.type",
    placeholder: "material.form.type_placeholder",
    valueKey: "id",
    labelKey: "name",
    filterable: true,
    options: []
  };

  const res = await getMaterialTypeList({
    start_page: 0,
    size: 0
  });

  select.options = res.data;

  return select;
};

export const materialSelect = async (key = "material_id") => {
  const select = {
    type: "select",
    key,
    label: "material.form.material_name",
    placeholder: "material.form.material_name_placeholder",
    valueKey: "material_id",
    labelKey: "name",
    options: []
  };

  const res = await getMaterialList({
    start_page: 0,
    size: 0
  });

  select.options = res.data;

  return select;
};
